﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using blackhouse.NemoBlog.Business;
using blackhouse.WebNemo.Business.Groups;
using log4net;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web;
using blackhouse;
using System.Configuration;
using mojoPortal.Web.Framework;
using mojoPortal.SearchIndex;

namespace blackhouse.NemoBlog.Web {
    public class PostIndexBuilderProvider : IndexBuilderProvider {

        #region Fields

        private SiteSettings site;
        private PageSettings page;
        private PageModule pageModule;
        private Module module;
        private NemoBlogSettings blogSetting;

        private static readonly ILog log = LogManager.GetLogger(typeof(PostIndexBuilderProvider));
        private static bool debugLog = PostIndexBuilderProvider.log.IsDebugEnabled;

        #endregion

        public override void ContentChangedHandler(object sender, ContentChangedEventArgs e) {
            if (sender == null)
                return;
            if (!(sender is GroupRecord))
                return;
            if (WebConfigSettings.DisableSearchIndex) {
                return;
            }
            if (WebConfigSettings.DisableSearchIndex) {
                return;
            }

            GroupRecord record = sender as GroupRecord;
            NemoBlogPost post = new NemoBlogPost(record.RecordId);
            int moduleId = record.GetInt32("moduleId");
            if (moduleId == 0) return;
            this.module = new Module(moduleId);
            this.pageModule = PageModule.GetPageModulesByModule(moduleId).FirstOrDefault();
            if (pageModule == null) return;
            this.site = new SiteSettings(this.module.SiteId);
            this.page = new PageSettings(site.SiteId, this.pageModule.PageId);
            this.blogSetting = new NemoBlogSettings(moduleId);

            if (e.IsDeleted) {
                IndexHelper.RemoveIndexItem(page.PageId, module.ModuleId, record.RecordId);
            }
            else
                if (post.IsPublished)
                    this.IndexItem(post);
        }

        private void IndexItem(NemoBlogPost post) {
            if (!page.IsPending) {
                IndexItem indexItem = new IndexItem();
                //if (content.SearchIndexPath.Length > 0) {
                //    indexItem.IndexPath = content.SearchIndexPath;
                //}
                indexItem.SiteId = site.SiteId;
                indexItem.PageId = blogSetting.RecordId;
                indexItem.PageName = blogSetting.BlogTitle;
                indexItem.ViewRoles = this.page.AuthorizedRoles;
                indexItem.ModuleViewRoles = this.module.ViewRoles;
                if (this.page.UseUrl) {
                    indexItem.ViewPage = post.PostUrl.ReplaceEx("~/", string.Empty);
                    indexItem.UseQueryStringParams = false;
                }
                if (ConfigurationManager.AppSettings["IndexPageMeta"] != null && ConfigurationManager.AppSettings["IndexPageMeta"] == "true") {
                    indexItem.PageMetaDescription = post.ShortDescription;
                }
                indexItem.FeatureId = module.FeatureName.ToString();
                indexItem.FeatureName = "Blog";
                indexItem.FeatureResourceFile = "";
                indexItem.ItemId = post.PostId;
                indexItem.ModuleId = post.ModuleId;
                indexItem.ModuleTitle = module.ModuleTitle;
                indexItem.Title = post.Title;
                indexItem.Content = SecurityHelper.RemoveMarkup(post.Content);
                indexItem.PublishBeginDate = post.PublishDate;
                //indexItem.PublishEndDate = current.PublishEndDate;
                if (!module.IncludeInSearch) {
                    indexItem.RemoveOnly = true;
                }
                IndexHelper.RebuildIndex(indexItem);
            }
        }

        public override void RebuildIndex(PageSettings pageSettings, string indexPath) {
            List<PageModule> pageModules = PageModule.GetPageModulesByPage(pageSettings.PageId);
            foreach (PageModule pm in pageModules) {
                this.module = new Module(pm.ModuleId);
                this.blogSetting = new NemoBlogSettings(pm.ModuleId);
                this.pageModule = pm;
                this.site = new SiteSettings(this.module.SiteId);
                this.page = new PageSettings(site.SiteId, this.pageModule.PageId);

                NemoBlogPost[] posts = NemoBlogPosts.GetPosts(pm.ModuleId, true);
                foreach (NemoBlogPost post in posts) {
                    this.IndexItem(post);
                }
            }
        }

    }
}